package com.torry.edss.domain.service.impl;

import com.torry.edss.application.cache.TradingDayCache;
import com.torry.edss.domain.accessor.*;
import com.torry.edss.domain.accessor.dto.FindLimitUpDTO;
import com.torry.edss.domain.annotations.TacticBean;
import com.torry.edss.domain.enums.TacticMode;
import com.torry.edss.domain.model.Company;
import com.torry.edss.domain.model.HisDailyLine;
import com.torry.edss.domain.model.LimitUp;
import com.torry.edss.domain.service.TacticService;
import com.torry.edss.domain.valueobject.MarketPlate;
import com.torry.edss.infras.util.DateUtil;
import com.torry.edss.infras.util.NumberUtils;
import com.torry.edss.integration.dingtalk.DingTalkClient;
import com.torry.edss.integration.facade.RemoteApiFacade;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 首板涨停，次日低开收阴，没有跌破涨停板最低位。缩量
 */
@TacticBean(mode = TacticMode.BAOLIXIPAN_v4)
@Slf4j
public class BaoLiXiPanTacticServiceImplV4 implements TacticService {
    public static int allCount = 0;
    public static int profitCount = 0;
    @Resource
    private LimitUpAccessor limitUpAccessor;
    @Resource
    private CompanyAccessor companyAccessor;
    @Resource
    private HisDailyLineAccessor hisDailyLineAccessor;
    @Resource
    private LimitDownAccessor limitDownAccessor;
    @Resource
    private LimitBombAccessor limitBombAccessor;
    @Resource
    private RemoteApiFacade remoteApiFacade;
    @Resource
    private DingTalkClient dingTalkClient;

    @Override
    public boolean realTimePicking() {
        return true;
    }

    @Override
    public boolean hisPicking(String date) {
        String limitDay = TradingDayCache.getPreviousTradingDay(TradingDayCache.getPreviousTradingDay(date));
        if (StringUtils.isBlank(limitDay)) return true;

        List<String> dates = TradingDayCache.getPreviousTradingDays(date, 2, false);
        List<LimitUp> limitUps = limitUpAccessor.findLimitUps(FindLimitUpDTO.builder()
                .dates(Collections.singletonList(dates.get(0))).continuousCounts(Collections.singletonList(1))
                .maxLtsz(10000000000D).onlyZb(true).includeSt(false).minLimitUpVolumeMultiple(1D).interrupted(true).build());

        for (LimitUp limitUp : limitUps) {
            Company company = companyAccessor.getById(limitUp.getCompanyId());
            HisDailyLine limitDayLine = hisDailyLineAccessor.getDayLine(company.getCode(), limitDay);
            HisDailyLine limitNextDayLine = hisDailyLineAccessor.getNextDayLine(company.getCode(), limitDay);

            HisDailyLine dayLine = hisDailyLineAccessor.getDayLine(company.getCode(), date);
            if (dayLine == null) continue;
            // 今日缩量
            if (dayLine.getVolume() > limitNextDayLine.getVolume() * 0.7) {
                continue;
            }
            // 首阴
//            if (limitNextDayLine.getHigh() > limitDayLine.getHigh()) {
//                continue;
//            }
            if (limitNextDayLine.getOpen() <= limitNextDayLine.getClose()) {
                continue;
            }
            if (dayLine.getHigh() > limitDayLine.getHigh()) {
                continue;
            }
            if (dayLine.getOpen() <= dayLine.getClose()) {
                continue;
            }

            List<HisDailyLine> allDayLines = hisDailyLineAccessor.findAllDayLines(company.getCode());
            int index = allDayLines.stream().map(e -> DateUtil.formatDate(e.getDate())).collect(Collectors.toList()).indexOf(date);

            double maxPrice = allDayLines.get(index).getClose();
            double minPrice = allDayLines.get(index).getClose();
            for (int i = index + 1; i <= index + 3; i++) {
                if (i == allDayLines.size()) {
                    break;
                }
                HisDailyLine dailyLine = allDayLines.get(i);
                if (dailyLine.getHigh() > maxPrice) {
                    maxPrice = dailyLine.getHigh();
                }
                if (dailyLine.getLow() < minPrice) {
                    minPrice = dailyLine.getLow();
                }
            }

            double profit = NumberUtils.format((maxPrice - allDayLines.get(index).getClose()) / allDayLines.get(index).getClose() * 100);
            double loss = NumberUtils.format((allDayLines.get(index).getClose() - minPrice) / allDayLines.get(index).getClose() * 100);
            allCount++;

            String msg = date + ", " + company.getCode() + ", " + company.getName() + ", 3日盈利: " + profit + " %, 5日亏损: " + loss + "%, 涨停量倍: "
                    + NumberUtils.format(limitDayLine.getVolume() / limitUp.getPreviousDayQuantity()) +
                    ", 首阴量倍: " + NumberUtils.format(dayLine.getVolume() / limitDayLine.getVolume()) + ", 统计: " + limitUp.getStat()
                    + "， 市值： " + (limitUp.getMarketValue() / 100000000) + "亿";

            if (profit > 5) {
                profitCount++;
                System.err.println(msg);
            } else {
                System.out.println(msg);
            }

        }
        return true;
    }

    @Override
    public boolean refreshProfit(Integer limit) {
        return false;
    }
}
